/*
对象属性去空
 */
function trimEmpty(obj){
    $.each(obj,function(key,val){
        if(val == null || typeof val == undefined){
            obj[key] = '';
        }else if(typeof val == "object"){
            trimEmpty(val);
        }
    });
    return obj;
}

/**
 * 格式化显示日期
 * @param now
 * @param mask
 * @returns {XML|void|string|*}
 */
function formatDate(now,mask)
{
    var d = now;
    var zeroize = function (value, length)
    {
        if (!length) length = 2;
        value = String(value);
        for (var i = 0, zeros = ''; i < (length - value.length); i++)
        {
            zeros += '0';
        }
        return zeros + value;
    };

    return mask.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g, function ($0)
    {
        switch ($0)
        {
            case 'd': return d.getDate();
            case 'dd': return d.dayOfMonth || zeroize(d.getDate());
            case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][d.getDay()];
            case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()];
            case 'M': return d.getMonth() + 1;
            case 'MM': return d.monthOfYear || zeroize(d.getMonth() + 1);
            case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()];
            case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()];
            case 'yy': return d.yearOfCentury || String(d.getFullYear()).substr(2);
            case 'yyyy': return d.year || d.getFullYear();
            case 'h': return d.getHours() % 12 || 12;
            case 'hh': return d.hourOfDay || zeroize(d.getHours() % 12 || 12);
            case 'H': return d.getHours();
            case 'HH': return d.hourOfDay;
            case 'm': return d.getMinutes();
            case 'mm': return d.minuteOfHour;
            case 's': return d.getSeconds();
            case 'ss': return d.secondOfMinute;
            case 'l': return zeroize(d.getMilliseconds(), 3);
            case 'L': var m = d.getMilliseconds();
                if (m > 99) m = Math.round(m / 10);
                return zeroize(m);
            case 'tt': return d.getHours() < 12 ? 'am' : 'pm';
            case 'TT': return d.getHours() < 12 ? 'AM' : 'PM';
            case 'Z': return d.toUTCString().match(/[A-Z]+$/);
            // Return quoted strings with the surrounding quotes removed
            default: return $0.substr(1, $0.length - 2);
        }
    });
};

/**
 * 获取上一个月
 *
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
function getPreMonth(date) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var year2 = year;
    var month2 = parseInt(month) - 2;
    if (month2 == -1) {
        year2 = parseInt(year2) - 1;
        month2 = 11;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = new Date(year2, month2, '01');
    return t2;
}

/**
 * 获取下一个月
 *
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
function getNextMonth(date) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var year2 = year;
    var month2 = parseInt(month);
    if (month2 == 12) {
        year2 = parseInt(year2) + 1;
        month2 = 0;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = new Date(year2, month2, '01');
    return t2;
}

/**
 * 获取月份内天数
 * @param date 日期
 */
function getDateByMonth(date) {
    var arr = date.split('-');
    var year = parseInt(arr[0]);
    var month = parseInt(arr[1]);
    var days;
    //当月份为二月时，根据闰年还是非闰年判断天数
    if (month == 2) {
        days = year % 4 == 0 ? 29 : 28;
    } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        //月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
        days = 31;
    } else {
        //其他月份，天数为：30.
        days = 30;
    }
    return days;
}

function renderOrderStatus(status) {
    if (status == -1) {
        return "待审核";
    } else if(status == 1) {
        return "审核通过,待接单";
    } else if(status == 2) {
        return "商家已接单";
    } else if(status == 3) {
        return "订单取消";
    }else if(status == 4) {
        return "申请退款";
    }else if(status == 5) {
        return "商家完成订单";
    }else if(status == 6) {
        return "用户确认完成订单";
    }else if(status == 7) {
        return "同意退款";
    }else if(status == 8) {
        return "拒绝退款";
    }else if(status == 9) {
        return "已评论";
    }else{
        return "未知状态";
    }
}

function renderPayType(payType) {
    if (payType == 1) {
        return "支付宝";
    } else if(payType == 2) {
        return "微信支付";
    }else if(payType == 6) {
        return "银联支付";
    }else{
        return "其他";
    }
}

function renderOrderPayStatus(payStatus) {
    if (payStatus == -1) {
        return "未付款";
    } else if(payStatus == 1) {
        return "已付定金,未付尾款";
    }else if(payStatus == 2) {
        return "已付定金和尾款";
    }else{
        return "未知状态";
    }
}

function renderProductStatus(status) {
    if (status == -1) {
        return "待审核或已下架";
    } else {
        return "审核通过";
    }
}

function renderExampleStatus(status) {
    if (status == -1) {
        return "待审核";
    } else if(status == 1){
        return "审核通过";
    }else if(status == 2){
        return "已上架";
    }else if(status == 3){
        return "已下架";
    }else if(status == 4){
        return "审核不通过";
    }else if(status == 0){
        return "待提交";
    }else if(status == 5){
        return "已推荐";
    }
}

function renderExampleType(status) {
    if (status == 1) {
        return "图片";
    } else if(status == 2){
        return "视频";
    }else if(status == 3){
        return "案例";
    }else{
        return "未知";
    }
}

function renderStatus(status) {
    if (status == -1) {
        return "待平台审核";
    } else {
        return "审核通过";
    }
}

function renderAuthType(authType) {
    if (authType == 1) {
        return "平台认证";
    } else if(authType == 2) {
        return "诚信认证";
    }else{
        return "学院认证";
    }
}

function createPage(data){
    var pageStr = '<div class="page"><a href="#" class="perPage"><em>&lt;&lt;</em></a>';
    for(var i = 0;i < data.totalPage;i++){
        pageStr += '<a href="#" class="pageNum">' + (i + 1) + '</a>';
    }
    pageStr += '<a href="#" class="nextPage"><em>&gt;&gt;</em></a>';
    pageStr += '<span>共<span>' + data.total + '</span>条/<span class="totalPage">' + data.totalPage + '</span>页</span>';

    $("#pageDiv").html(pageStr);
    $(".pageNum").each(function(i,emel){
        if($(this).html() == data.nowPage){
            $(this).addClass("cur");
            $(this).addClass("nowPage");
        }
    });

    $(".pageNum").click(function(){
        goPage($(this).html());
    });

    $(".perPage").click(function(){
        goPage(($(".nowPage").html() - 0) - 1);
    });

    $(".nextPage").click(function(){
        goPage(($(".nowPage").html() - 0) + 1);
    });
}

function goPage(nowPage){
    nowPage = nowPage <= 1 || isNaN(nowPage) ? 1 : nowPage;
    var totalPage = $(".totalPage").html() - 0;
    nowPage = nowPage >= totalPage ? totalPage < 1 ? 1 : totalPage : nowPage;
    // var pageParam = $.extend(staticParam,{"curPage":nowPage});
    var pageParam = {"curPage":nowPage};
    getListPage(pageParam);
}

/**
 * 获取商家职业
 * @param id
 * @returns {string}
 */
function getBusinessProfession(id){
    var option = '<option value="">请选择</option>';
    $.ajax({
        type:'get',
        dataType:'json',
        url:'/systemRole/findAllRoleByTypePage.json',
        data: {roleType:2,page:1,pageNum:10000},
        async: false,
        success:function (data) {
            $(data.roleList).each(function(i,role){
                option += '<option value="' + role.roleName + '">' + role.roleName +'</option>'
            });
        }
    });
    $("#" + id).html(option);
    return option;
}

/**
 * 获取字典表
 * @param type  1  婚礼类型  2  婚礼环境
 * @returns {{}}
 */
function getDictByType(type){
    var obj = {};
    $.ajax({
        type:'get',
        dataType:'json',
        url:'/htmlWeb/getDictListByPage',
        data: {type:type,curPage:1,pageSize:10000},
        async: false,
        success:function (data) {
            if(data){
                obj = data.rows;
            }
        }
    });
    return obj;
}

/**
 * 图片预览
 * @param avalue
 * @returns {boolean}
 */
function setImagePreview(avalue) {
    var docObj=document.getElementById("f");

    var imgObjPreview=document.getElementById("preview");
    if(docObj.files &&docObj.files[0])
    {
//火狐下，直接设img属性
//         imgObjPreview.style.display = 'block';
//         imgObjPreview.style.width = '150px';
//         imgObjPreview.style.height = '180px';
//imgObjPreview.src = docObj.files[0].getAsDataURL();

//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
        imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
    }
    else
    {
//IE下，使用滤镜
        docObj.select();
        var imgSrc = document.selection.createRange().text;
        var localImagId = document.getElementById("localImag");
//必须设置初始大小
        localImagId.style.width = "150px";
        localImagId.style.height = "180px";
//图片异常的捕捉，防止用户修改后缀来伪造图片
        try{
            localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
        }
        catch(e)
        {
            alert("您上传的图片格式不正确，请重新选择!");
            return false;
        }
        imgObjPreview.style.display = 'none';
        document.selection.empty();
    }
    return true;
}

/**
 * 截取省略多余字段
 * @param data  原始字符串
 * @param length  显示长度
 * @param suffix  后缀
 * @returns {*}
 */
function subStr(data,length,suffix) {
    var len = 20,suf = "......";
    if(length){
        len = length;
    }
    if(suffix){
        suf = suffix;
    }
    if(data.length >= len){
        return data.substr(0,len) + suf;
    }else{
        return data;
    }
}

/**
 * 后台弹出告警框
 * @param content
 */
function tip(content){
    if($(".deskDiv").length < 1){
        var desk = '<div class="deskDiv"></div>';
        $("body").append(desk);
    }
    if($("#hintsPValue").length < 1){
        var element = '<div class="hints layerDele"><h2 class="layerTit">友情提示</h2><div class="layerDiv"><p id="hintsPValue">' + content + '</p>' +
            '<h1 class="layerH1"><span class="confirm">确认</span></h1></div></div>';
        $("body").append(element);
        $(".cancel,.confirm").click(function(){
            $(".hints").fadeOut();
            $(".deskDiv").fadeOut();
        });
    }else{
        $("#hintsPValue").html(content);
    }

    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
    $(".deskDiv").css({
        display:"block"
    });
}